using System.Collections;
using Concurso;
using Concurso.Clases;
using Curriculum.Services;
using Utiles;

namespace ConcursoCV.UI.WebUserControls.AdmVersionesCV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ListadoDeAtributos.
	/// </summary>
	public class ListadoDeAtributos : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox txtNombre;
		protected System.Web.UI.WebControls.Button btnBuscar;
		protected System.Web.UI.WebControls.Button btnLimpiar;
		protected System.Web.UI.WebControls.Button btnNew;
		protected System.Web.UI.WebControls.DropDownList ddlTipo;
		protected System.Web.UI.WebControls.Label lblCount;
		protected System.Web.UI.WebControls.DataGrid dtgAtributos;

		private const string pidABMAtributos="012";

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{				
				InitializeVariables();				
				PopulateCombo();				
			}
		}
		private void PopulateCombo()
		{
			PopulateComboTipos();
		}
		private void PopulateComboTipos()
		{
			VersionMetadatosService svc = new VersionMetadatosService();			
			Helper.ListHelper.FillDropDownList(ddlTipo, "Id", "Valor", svc.GetTiposDatos(), true);
		}

		private void InitializeVariables()
		{
			SortColumn = "Nombre";
			SortDirection = true;			
		}

		private void FillSearchVariables() 
		{
			FiltroNombre = txtNombre.Text.Trim() != string.Empty ? txtNombre.Text.Trim() : string.Empty;
			FiltroTipoId = ddlTipo.SelectedIndex > 0 ? ddlTipo.SelectedValue  : string.Empty;
		}

		private IList GetListBySearch() 
		{
			// Search			
			VersionMetadatosService svc = new VersionMetadatosService();
			return svc.GetAtributos(FiltroNombre, FiltroTipoId);
		}


		private void FillDataGridWithSearch()
		{
			IList list = GetListBySearch();
			((ArrayList)list).Sort(new GenericComparer(SortColumn,SortDirection));
			dtgAtributos.DataSource = list;
			lblCount.Text = list.Count.ToString();
			try
			{
				dtgAtributos.DataBind();
			}
			catch(Exception exc)
			{
				string msg = exc.Message;
				dtgAtributos.CurrentPageIndex = 0;
				FillDataGridWithSearch();
			}

			list = null;
		}

		private void RedirectToAtributoABM(PageMode mode, string atributoId)
		{
			string url = "Default.aspx?pid=" + pidABMAtributos + "&atributoId=" + atributoId + "&PageMode=" + PageModeConverter.ToInt( mode );
			Server.Transfer(url);
		}

		private void EliminarAtributo(string atributoId)
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			svc.EliminarAtributoMetadato(atributoId);
		}

		#region - properties -

		public string FiltroNombre
		{
			get{ return (string)ViewState["_filtroNombre"];}
			set{ ViewState["_filtroNombre"] = value;}
		}
		public string FiltroTipoId
		{
			get{ return (string)ViewState["_filtroTipoId"];}
			set{ ViewState["_filtroTipoId"] = value;}
		}

		#region - Sort Variables -

		public string SortColumn
		{
			get{ return (string)ViewState["SortColumn"];}
			set{ ViewState["SortColumn"] = value;}
		}
		public bool SortDirection
		{
			get{ return (bool)ViewState["SortDirection"];}
			set{ ViewState["SortDirection"] = value;}
		}

		#endregion - Sort Variables -

		#endregion - properties -

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnBuscar.Click += new System.EventHandler(this.btnBuscar_Click);
			this.btnLimpiar.Click += new System.EventHandler(this.btnLimpiar_Click);
			this.dtgAtributos.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgAtributos_ItemCommand);
			this.dtgAtributos.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgJurados_PageIndexChanged);
			this.dtgAtributos.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgAtributos_SortCommand);
			this.btnNew.Click += new System.EventHandler(this.btnNew_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnBuscar_Click(object sender, System.EventArgs e)
		{
			dtgAtributos.CurrentPageIndex=0;
			FillSearchVariables();
			FillDataGridWithSearch();
		}

		private void btnLimpiar_Click(object sender, System.EventArgs e)
		{
			txtNombre.Text = string.Empty;
			ddlTipo.SelectedIndex = 0;
		}

		private void btnNew_Click(object sender, System.EventArgs e)
		{
			string url = "Default.aspx?pid=" + pidABMAtributos;
			Server.Transfer(url);
		}

		private void dtgJurados_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgAtributos.CurrentPageIndex = e.NewPageIndex;
			FillDataGridWithSearch();		
		}

		private void dtgAtributos_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" ) 
			{
				string atributoId = e.Item.Cells[0].Text;				
				switch ( e.CommandName ) 
				{
					case "Ver":
						RedirectToAtributoABM(PageMode.VIEW,atributoId);
						break;

					case "Modificar":
						RedirectToAtributoABM(PageMode.EDIT,atributoId);
						break;
						
					case "Eliminar":
						EliminarAtributo(atributoId);	
						FillDataGridWithSearch();
						break;
				}
			}					
		}

		private void dtgAtributos_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			//Si se hace click sobre ,la misma columna, cambia el orden
			if (SortColumn.ToString() == e.SortExpression.ToString())
				SortDirection = !SortDirection;
			
			SortColumn = e.SortExpression;
			FillDataGridWithSearch();		
		}
	}
}
